package cn.hh.wechatkit.component;

import cn.hh.wechatkit.api.card.*;
import cn.hh.wechatkit.data.postdata.card.Meta_CardSeatPostData;
import cn.hh.wechatkit.data.postdata.card.meet.Meta_MeetCard;
import cn.hh.wechatkit.data.returndata.Meta_RMsg_Base;
import cn.hh.wechatkit.data.returndata.card.Meta_RData_CardState;
import cn.hh.wechatkit.data.returndata.card.Meta_RData_CreateCard;
import cn.hh.wechatkit.data.returndata.card.Meta_RData_MeetingCardInfo;
import cn.hh.wechatkit.exception.*;
import cn.hh.wechatkit.helper.CacheHelper;
import cn.hh.wechatkit.pojo.card.Wx_Card;
import cn.hh.wechatkit.pojo.card.Wx_CardExt;

import java.io.IOException;

/**
 * Created this one by HMH on 2017/5/23.
 * 卡券管理组件
 */
public class WxCom_Card {

    /**
     * 生成会议卡券
     */

    public Meta_RData_CreateCard generateMeetCard(Meta_MeetCard card) throws Wx_Exception_GetTokenFailed, Wx_Exception_InvalidResponse, Wx_Exception_InvalidJsonParse, IOException {
        return WxAPI_Card_Create_Api.createWxCard(card, CacheHelper.getAccessToken().getAccess_token());
    }

    /**
     * 核销卡券
     */

    public Meta_RMsg_Base consumeCard(String cardId, String code){

        Meta_RData_CardState stateMsg = WxAPI_Card_Check_Api.check(cardId, code, CacheHelper.getAccessToken().getAccess_token());
        if (stateMsg == null) {
            throw new Wx_Exception_BadNetwork("网络失败");
        }
        if (stateMsg.getErrcode() != 0) {
            throw new Wx_Exception_BadCard(stateMsg.getErrmsg());
        }
        if(!stateMsg.isCan_consume()){
            throw new Wx_Exception_BadCard("该卡券无法核销，可能已经失效");
        }
        Meta_RMsg_Base msg = WxAPI_Card_Consume_Api.consume(code, CacheHelper.getAccessToken().getAccess_token());
        if (msg == null) {
            throw new Wx_Exception_OperationFalied("核销卡券失败");
        } else if (msg.getErrcode() != 0) {
            throw new Wx_Exception_OperationFalied("核销卡券失败: "+msg.getErrmsg() );
        }
        return msg;
    }


    /**
     * 查询卡券详细信息
     */
    public Meta_RData_MeetingCardInfo getCardInfo(String cardId) {
        return WxAPI_Card_GetCardInfo_Api.getInfo(cardId, CacheHelper.getAccessToken().getAccess_token());
    }

    /**
     * 更新卡券信息 - 数量
     */
    public Meta_RMsg_Base updateQuantity(String cardId, String action, int number) throws Exception {
        Meta_RMsg_Base msg = WxAPI_Card_UpdateCard_Api.updateQuantity(cardId, action, number, CacheHelper.getAccessToken().getAccess_token());
        if (msg == null) {
            throw new Wx_Exception_OperationFalied("更新数量失败");
        } else if (msg.getErrcode() != 0) {
            throw new Wx_Exception_OperationFalied("更新数量失败: "+msg.getErrmsg() );
        }
        return msg;
    }

    //更新卡券座位
    public Meta_RMsg_Base updateSeat(Meta_CardSeatPostData seat) {
        Meta_RMsg_Base msg = WxAPI_Card_UpdateSeat_Api.updateSeat(seat, CacheHelper.getAccessToken().getAccess_token());
        if (msg == null) {
            throw new Wx_Exception_OperationFalied("更新座位失败");
        } else if (msg.getErrcode() != 0) {
            throw new Wx_Exception_OperationFalied("更新座位失败: "+msg.getErrmsg() );
        }
        return msg;
    }

    //生成卡券签名
    public Wx_CardExt generateCardExt(String cardId) throws IOException {
        return Wx_CardExt.generateCardExt(CacheHelper.getApiTicketToken().getTicket(), cardId);
    }

    //销毁卡券
    public Meta_RMsg_Base cancelCard(String cardId){
        Meta_RMsg_Base msg = WxAPI_Card_CancelCard_API.cancelCard(cardId, CacheHelper.getAccessToken().getAccess_token());
        if (msg == null) {
            throw new Wx_Exception_OperationFalied("卡券销毁失败");
        } else if (msg.getErrcode() != 0) {
            throw new Wx_Exception_OperationFalied("卡券销毁失败：" + msg.getErrmsg());
        }
        return msg;
    }

    //销毁票券
    public Meta_RMsg_Base cancelTicket(String code,String reason) {
        Meta_RMsg_Base msg = WxAPI_Card_CancelCard_API.cancelTicket(code, reason, CacheHelper.getAccessToken().getAccess_token());
        if (msg == null) {
            throw new Wx_Exception_OperationFalied("票券销毁失败");
        } else if (msg.getErrcode() != 0) {
            throw new Wx_Exception_OperationFalied("票券销毁失败：" + msg.getErrmsg());
        }
        return msg;
    }


}